<?php

  include "DALorder.php";
    
  //insert a new record of order details
  function addOrderDetails($c_no, $o_products, $o_freight, $o_merchanise, $o_delivery, $o_insurance, $o_total, $o_card)
  {
    $productItems = setItemsReserve($o_products);
    if($productItems != "0")
    {
      if(addOrderDetailsDB($c_no, $productItems, $o_freight, $o_merchanise, $o_delivery, $o_insurance, $o_total, $o_card) == true)
      {
        return true;
      }
      else
      {
        echo "<SCRIPT language='Javascript'>";
        echo "  alert('The order could not be saved.  Please try again.');";
        echo "</SCRIPT>";
        return false;
      }
    }
    else
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The stock levels has changed and the items could not be reserved.  Please re-order.');";
      echo "</SCRIPT>";
      return false;
    }
  }
  
  function validateOrderDetails($product, $freight, $merchanise, $delivery, $insurance, $total, $card_details)
  {
    $j = 0;
    $k = 0;
    
    for($i=0; $i < sizeof($product); $i+=2)
    {
      if($product[$i] == "" || empty($product[$i]))
        $j++;
      else
      {
        $orderProducts[$k] = $product[$i];
        $k++;
        $orderProducts[$k] = $product[$i+1];
        $k++;
      }
    }
 
    if($j == 10)
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('You must enter and order at least one product.');";
      echo "</SCRIPT>";
      return "0";
    }
    
    if($freight == "0")
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('You must select a delivery option.');";
      echo "</SCRIPT>";
      return "0";
    }
    
    if($card_details[0] == "0")
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('You must enter in the card type if the payment option is \"other\".');";
      echo "</SCRIPT>";
      return "0";
    }

    if($card_details[1] == "" || empty($card_details[1]))
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('You must enter in the card name if the payment option is \"other\".');";
      echo "</SCRIPT>";
      return "0";
    }

    if(strlen($card_details[2]) != 4 || strlen($card_details[3]) != 4 || strlen($card_details[4]) != 4 || strlen($card_details[5]) != 4)
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The credit card number sections must be 4 digits long.');";
      echo "</SCRIPT>";
      return "0";
    }
    
    if(isDigit($card_details[2]) == false || isDigit($card_details[3]) == false  || isDigit($card_details[4]) == false || isDigit($card_details[5]) == false)
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The credit card number must be numeric.');";
      echo "</SCRIPT>";
      return "0";
    }
    
    return $orderProducts;

  }
  
  function isDigit($number)
  {
    for($i = 0; $i < strlen($number); $i++)
    {
      $n = $number[$i];
      if($n == '0' || $n == '1' || $n == '2' || $n == '3' || $n == '4' || $n == '5' || $n == '6' || $n == '7' || $n == '8' || $n == '9')
      {
      }
      else
      {
        return false;
      }
    }
    return true;
  }

  //update the record of the order to cancel
  function cancelOrder($order_no, $c_no)
  {
    return cancelOrderDB($order_no, $c_no);
  }

  //retrieves the record of the order
  function getOrderDetails($custID, $order_no, $order_date, $prod_no)
  {
    if($order_no != "")
    {
      $data = getOrderDetailsOrderNo($custID, $order_no);
    }
    else if($prod_no != "")
    {
      $data = getOrderDetailsProdNo($custID, $prod_no);
    }
    else if($order_date != "")
    {
      $data = getOrderDetailsOrderDate($custID, $order_date);
    }
    else
    {
      $data = getOrderDetailsAll($custID);
    }

    if($data[0][0] == "")
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('No orders found for this customer.');";
      echo "  window.location.href = 'enquiryOnOutstandingOrder_1ordersSearch.php';";
      echo "</SCRIPT>";
    }

    // first row is done outside for loop to avoid out of array bounds exception
    if($data[0][2] == "P")
    {
      echo "<tr><td align='center' style='border:1px solid #000000; background-color:#CCCCCC;'><input class='orderResults' name='order' type='submit' style=\"font-family:'Century Gothic'; font-size:12px; color:#FFFFFF; border:1px solid #000000; width:20px; background-color:#FFFFFF\" value='".$data[0][0]."'></td>";
    }
    else
    {
      echo "<tr><td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    }
    for($i = 0; $i < 2; $i++)
    {
      echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>".$data[0][$i]."</td>";
    }
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>$".$data[0][10]."</td>";
    echo "</tr>";

    echo "<tr><td style='border:1px solid #000000'>&nbsp</td>";
    echo "<td style='border:1px solid #000000'>&nbsp</td>";
    echo "<td style='border:1px solid #000000'>&nbsp</td>";
    for($i = 2; $i < 10; $i++)
    {
      echo "<td style='border:1px solid #000000'>".$data[0][$i]."</td>";
    }
    echo "</tr>";

    // covers the rest of the data
    for($i = 1; $i < sizeof($data); $i++)
    {
      // handles the seperation of orders
      if($data[$i][0] != $data[($i - 1)][0])
      {
        if($data[$i][2] == "P")
        {
          echo "<tr><td align='center' style='border:1px solid #000000; background-color:#CCCCCC;'><input class='orderResults' name='order' type='submit' style=\"font-family:'Century Gothic'; font-size:12px; color:#FFFFFF; border:1px solid #000000; width:20px; background-color:#FFFFFF\" value='".$data[$i][0]."'></td>";
        }
        else
        {
          echo "<tr><td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
        }
        for($j = 0; $j < 2; $j++)
        {
          echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>".$data[$i][$j]."</td>";
        }
        echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
        echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
        echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
        echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
        echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
        echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
        echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
        echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>$".$data[$i][10]."</td>";
        echo "</tr>";
      }
      echo "<tr><td style='border:1px solid #000000'>&nbsp</td>";
      echo "<td style='border:1px solid #000000'>&nbsp</td>";
      echo "<td style='border:1px solid #000000'>&nbsp</td>";
      for($j = 2; $j < 10; $j++)
      {
        echo "<td style='border:1px solid #000000'>".$data[$i][$j]."</td>";
      }
      echo "</tr>";
    }
  }

  function validSearchDetailsCust($surname, $given_name, $dob_day, $dob_month, $dob_year, $address)
  {
    if($surname == "" || $given_name == "" || $dob_day == "" || $dob_month == "" || $dob_year == "" || $address == ""
      || empty($surname) || empty($given_name) || empty($dob_day) || empty($dob_month) || empty($dob_year) || empty($address))
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('You must enter details in all mandatory fields.');";
      echo "</SCRIPT>";
      return false;
    }
    else if(strlen($surname) > 30)
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The surname must be less than or equal to 30 characters long.');";
      echo "</SCRIPT>";
      return false;
    }
    else if(strlen($given_name) > 30)
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The given name must be less than or equal to 30 characters long.');";
      echo "</SCRIPT>";
      return false;
    }
    else if(strlen($address) > 50)
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The address must be less than or equal to 50 characters long.');";
      echo "</SCRIPT>";
      return false;
    }
    else
    {
      $dob = $dob_year."-".$dob_month."-".$dob_day;

      if(checkCustomerDetailsCust($surname, $given_name, $dob, $address) == true)
      {
        echo "<SCRIPT language='Javascript'>";
        echo "  alert('The customer is not registered.');";
        echo "</SCRIPT>";
        return false;
      }
      else
      {
        return true;
      }
    }
  }
  
  function getOneOrderDetails($custID, $order_no)
  {
    $data = getOrderDetailsOrderNo($custID, $order_no);

    for($i = 0; $i < 2; $i++)
    {
      echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>".$data[0][$i]."</td>";
    }
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>&nbsp</td>";
    echo "<td style='border:1px solid #000000; background-color:#CCCCCC;'>$".$data[0][10]."</td>";
    echo "</tr>";

    for($i = 0; $i < sizeof($data); $i++)
    {
      echo "<tr><td style='border:1px solid #000000'>&nbsp</td>";
      echo "<td style='border:1px solid #000000'>&nbsp</td>";
      for($j = 2; $j < 10; $j++)
      {
        echo "<td style='border:1px solid #000000'>".$data[$i][$j]."</td>";
      }
      echo "</tr>";
    }
  }

  function validOrderSearchDetails($order_no, $prod_no)
  {
    if(isDigit($order_no) == false)
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The Order No. must be numeric.');";
      echo "</SCRIPT>";
      return false;
    }
    else if(isDigit($prod_no) == false)
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The Product No. must be numeric.');";
      echo "</SCRIPT>";
      return false;
    }
    return true;
  }

?>